********************************************************************************
** Do file: g6_admin_demos.do
** First started: December 14, 2020 
** Last edited: September 8, 2023

/* Purpose: this do file brings together demographic information coming from the 
administrative data as well as 2019 LSMS data.

Part I: Set-up survey data from experiment and further set up data for analysis
Part II: Descriptives
Part III: Tests of difference

* ITEM MADE: TABLE A.1
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

************
** Part I **
************

* Load key dataset
use "$merged/key_rep.dta", clear

* Rename variables
ren age_a age
ren numberofchildren_a numberofchildren
ren female_a female
ren married_a married
ren occupation_cat_a occupation_cat
ren region_cat_a region_cat

* Generate dummies
tab occupation_cat, gen(occ_cat)
tab region_cat, gen(reg)

* Label Fenix data
g fenixdata = 1

* Append lsms information, but must first manage
preserve
	use "$lsms2018/lsms2018_vars.dta", clear
	ren hhid accountid
	tempfile lsms
	save `lsms'
restore
append using `lsms'
replace fenixdata = 0 if fenixdata==.

* Change
replace accountpercentlocked_may = accountpercentlocked_may/100



***** TABLE A.1 *****

*************
** Part II **
*************

* LSMS
count if fenixdata == 0

reg age if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg female if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg married if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg numberofchildren if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg occ_cat1 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg occ_cat2 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg occ_cat3 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg occ_cat4 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg reg1 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg reg2 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')

reg reg3 if fenixdata == 0 [pweight=wgt]
display _se[_cons]*sqrt(`e(N)')


* SMS#1 sent to how many people? *
count if k_complete_may==1
sum accountpercentlocked_may if k_complete_may==1
sum age if k_complete_may==1
sum female if k_complete_may==1
sum married if k_complete_may==1 
sum numberofchildren if k_complete_may==1 
tab occupation_cat if k_complete_may==1 
	sum occ_cat1 if k_complete_may==1
	sum occ_cat2 if k_complete_may==1
	sum occ_cat3 if k_complete_may==1
	sum occ_cat4 if k_complete_may==1
tab region if k_complete_may==1 
	sum reg1 if k_complete_may==1
	sum reg2 if k_complete_may==1
	sum reg3 if k_complete_may==1
count if k_complete_may==1 & treatmenttype_sh=="R T3"
display 63/27081

	
* How many people had received (any) loan? *
* Note that control group is included below
* Overall
count if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
sum accountpercentlocked_may if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
sum age if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
sum female if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
sum married if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
sum numberofchildren if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
tab occupation_cat if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1
	sum occ_cat1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
	sum occ_cat2 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
	sum occ_cat3 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
	sum occ_cat4 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
tab region if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1
	sum reg1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
	sum reg2 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
	sum reg3 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
count if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh=="R T3"
display 16/1072

	
**************
** Part III **
**************


** Comparing LSMS to SMS sent to **
* Age
reg age if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local sd1 = _se[_cons]*sqrt(`e(N)')
local n1 = `e(N)'

display `mean1'
display `sd1'
display `n1'

sum age if k_complete_may==1
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local t = (`mean1'-`mean2')/sqrt(((`n1'-1)*`sd1'^2+(`n2'-1)*`sd2'^2)/(`n1'+`n2'-2)*(1/`n1'+1/`n2'))
local abs_t = abs(`t')
local df = `n1'+`n2'-2
display ttail(`df',`abs_t')*2

* Female
reg female if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum female if k_complete_may==1
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Married
reg married if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum married if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Number of children
reg numberofchildren if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local sd1 = _se[_cons]*sqrt(`e(N)')
local n1 = `e(N)'

display `mean1'
display `sd1'
display `n1'

sum numberofchildren if k_complete_may==1
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local t = (`mean1'-`mean2')/sqrt(((`n1'-1)*`sd1'^2+(`n2'-1)*`sd2'^2)/(`n1'+`n2'-2)*(1/`n1'+1/`n2'))
local abs_t = abs(`t')
local df = `n1'+`n2'-2
display ttail(`df',`abs_t')*2

* Occupation - agriculture or non-employed
reg occ_cat1 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum occ_cat1 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Occupation - nonprofessional
reg occ_cat2 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum occ_cat2 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Occupation - other
reg occ_cat3 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum occ_cat3 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Occupation - professional
reg occ_cat4 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum occ_cat4 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Region - central 
reg reg1 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum reg1 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Region - eastern
reg reg2 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum reg2 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Region - wastern
reg reg3 if fenixdata == 0 [pweight=wgt]
local mean1 = _b[_cons]
local n1 = `e(N)'

sum reg3 if k_complete_may==1 
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2




** Comparing SMS sent to to Took loan **
* Proportion of days locked at SMS
sum accountpercentlocked_may if k_complete_may==1
local mean1 = r(mean)
local sd1 = r(sd)
local n1 = r(N)

sum accountpercentlocked_may if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local t = (`mean1'-`mean2')/sqrt(((`n1'-1)*`sd1'^2+(`n2'-1)*`sd2'^2)/(`n1'+`n2'-2)*(1/`n1'+1/`n2'))
local abs_t = abs(`t')
local df = `n1'+`n2'-2
display ttail(`df',`abs_t')*2

* Age
sum age if k_complete_may==1
local mean1 = r(mean)
local sd1 = r(sd)
local n1 = r(N)

sum age if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local t = (`mean1'-`mean2')/sqrt(((`n1'-1)*`sd1'^2+(`n2'-1)*`sd2'^2)/(`n1'+`n2'-2)*(1/`n1'+1/`n2'))
local abs_t = abs(`t')
local df = `n1'+`n2'-2
display ttail(`df',`abs_t')*2

* Female
sum female if k_complete_may==1
local mean1 = r(mean)
local sd1 = r(sd)
local n1 = r(N)

sum female if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Married
sum married if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum married if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Number of children
sum numberofchildren if k_complete_may==1
local mean1 = r(mean)
local sd1 = r(sd)
local n1 = r(N)

sum numberofchildren if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local sd2 = r(sd)
local n2 = r(N)

local t = (`mean1'-`mean2')/sqrt(((`n1'-1)*`sd1'^2+(`n2'-1)*`sd2'^2)/(`n1'+`n2'-2)*(1/`n1'+1/`n2'))
local abs_t = abs(`t')
local df = `n1'+`n2'-2
display ttail(`df',`abs_t')*2

* Occupation - agriculture or non-employed
sum occ_cat1 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum occ_cat1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Occupation - nonprofessional
sum occ_cat2 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum occ_cat2 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Occupation - other
sum occ_cat3 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum occ_cat3 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Occupation - professional
sum occ_cat4 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum occ_cat4 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Region - central or northern
sum reg1 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum reg1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Region - eastern
sum reg2 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum reg2 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2

* Region - western
sum reg3 if k_complete_may==1
local mean1 = r(mean)
local n1 = r(N)

sum reg3 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & treatmenttype_sh!="R T3"
local mean2 = r(mean)
local n2 = r(N)

local z = (`mean1'-`mean2')/sqrt(((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2'))*(1-((`n1'*`mean1'+`n2'*`mean2')/(`n1'+`n2')))*(1/`n1'+1/`n2'))
local abs_z = abs(`z')
display (1-normal(`abs_z'))*2
